﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    public class CLSTABCAU
    {
        string codcau_cau, nombre_cau;
        MDOgeneral mod = new MDOgeneral();

        public string CODCAU_CAU
        {
            get { return codcau_cau; }
            set { codcau_cau = value; }
        }

        public string NOMBRE_CAU
        {
            get { return nombre_cau; }
            set { nombre_cau = value; }
        }

        #region Metodos

        public List<CLSTABCAU> listarCausas()
        {

            try
            {
                string query = "select codcau_cau, nombre_cau from tabcau order by nombre_cau ";
                DataTable val = mod.consulta(query);
                List<CLSTABCAU> liscau = new List<CLSTABCAU>();
                foreach (DataRow r in val.Rows)
                {
                    liscau.Add(new CLSTABCAU
                    {
                        CODCAU_CAU = r[0].ToString(),
                        NOMBRE_CAU = r[1].ToString()
                    });
                }
                return liscau;
            }
            catch (Exception ex) { throw (ex); }
        }
        
        public DataTable consLisCau() 
        {
            try 
            {
                string query = "select TRIM(CODCAU_CAU) as  CODCAU_CAU, TRIM(NOMBRE_CAU) as NOMBRE_CAU from tabcau order by NOMBRE_CAU";
                return mod.consulta(query);
            }
            catch(Exception ex){throw ex;}
        }

        public int Insertar()
        {
            try
            {
                string query = "INSERT INTO TABCAU(CODCAU_CAU, NOMBRE_CAU) VALUES ('" + CODCAU_CAU + "','" + NOMBRE_CAU + "')";
                return mod.comando(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public int Actualizar()
        {
            try
            {
                string query = "update tabcau set nombre_cau='" + NOMBRE_CAU + "' where codcau_cau='" + CODCAU_CAU + "'";
                return mod.comando(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public int Eliminar()
        {
            try
            {
                string query = "delete from tabcau where codcau_cau='" + CODCAU_CAU + "'";
                return mod.comando(query);
            }
            catch (Exception ex) {  throw ex; }
        }

        public DataTable Consultar()
        {
            try
            {
                string query = "select codcau_cau, nombre_cau from tabcau order by nombre_cau";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public Boolean Existe()
        {
            try
            {
                string query = "select codcau_cau from tabcau where codcau_cau='" + CODCAU_CAU + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex) { throw ex; }
        }

        #endregion
    }
}
